<?php if ($form->hasGlobalErrors()): ?>
    <div class="alert alert-error">
        <?php foreach ($form->getGlobalErrors() AS $error) : ?>
            <?php echo $error ?>
        <?php endforeach; ?>
    </div>
<?php endif ?>

<div class="portlet box blue">
    <div class="portlet-title">
        <div class="caption"><i class="icon-reorder"></i>Зургууд</div>
        <div class="tools">
            <a href="javascript:;" class="collapse"></a>
        </div>
    </div>

    <div class="portlet-body">
        <form id="fileupload" action="<?php echo url_for('@product_photo_uploader') ?>" method="POST" enctype="multipart/form-data">
            <div class="row-fluid fileupload-buttonbar">
                <div class="span7">
                    <span class="btn green fileinput-button">
                        <i class="icon-plus icon-white"></i>
                        <span>Зургууд нэмэх...</span>
                        <input type="file" name="files[]" multiple>
                    </span>
                    <button type="submit" class="btn blue start">
                        <i class="icon-upload icon-white"></i>
                        <span>Хуулж эхлэх</span>
                    </button>
                    <button type="reset" class="btn yellow cancel">
                        <i class="icon-ban-circle icon-white"></i>
                        <span>Хуулалтыг зогсоох</span>
                    </button>
                    <button type="button" class="btn red delete">
                        <i class="icon-trash icon-white"></i>
                        <span>Устгах</span>
                    </button>
                    <input type="checkbox" class="toggle fileupload-toggle-checkbox">
                </div>
                <div class="span5 fileupload-progress fade">
                    <div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                        <div class="bar" style="width:0%;"></div>
                    </div>
                    <div class="progress-extended">&nbsp;</div>
                </div>
            </div>
            <div class="fileupload-loading"></div>
            <br>
            <table role="presentation" class="table table-striped">
                <tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody>
            </table>
        </form>

        <div class="row-fluid">
            <div class="span12">
                <script id="template-upload" type="text/x-tmpl">
                    {% for (var i=0, file; file=o.files[i]; i++) { %}
                    <tr class="template-upload fade">
                    <td>
                    <input type="hidden" class="photo_ids" value="{%=file.id%}">
                    <span class="preview"></span>
                    </td>

                    <td>
                    <p class="name">{%=file.name%}</p>
                    <input name="product_id[]" type="hidden" value="<?php echo $id ?>" class="form-control">
                    <strong class="error text-danger"></strong>
                    </td>

                    <td>
                    <p class="size">Processing...</p>
                    <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
                    </td>

                    <td>
                    {% if (!i && !o.options.autoUpload) { %}
                    <button class="btn btn-primary start" disabled>
                    <i class="glyphicon glyphicon-upload"></i>
                    <span>Эхлэх</span>
                    </button>
                    {% } %}
                    {% if (!i) { %}
                    <button class="btn btn-warning cancel">
                    <i class="glyphicon glyphicon-ban-circle"></i>
                    <span>Устгах</span>
                    </button>
                    {% } %}
                    </td>
                    </tr>
                    {% } %}
                </script>

                <script id="template-download" type="text/x-tmpl">
                    {% for (var i=0, file; file=o.files[i]; i++) { %}
                    <tr class="template-download fade">
                    <td>
                    <input type="hidden" class="photo_ids" value="{%=file.id%}">
                    <span class="preview">
                    {% if (file.thumbnailUrl) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
                    {% } %}
                    </span>
                    </td>
                    <td>
                    <p class="name">
                    {% if (file.url) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a>
                    {% } else { %}
                    <span>{%=file.name%}</span>
                    {% } %}
                    </p>
                    {% if (file.error) { %}
                    <div><span class="label label-danger">Error</span> {%=file.error%}</div>
                    {% } %}
                    <a href="javascript:" onclick="makeCover(this, {%=file.id%})" class="cover-maker tip-bottom" title="Үндсэн зураг болгох"><i class="icon-ok-sign icon-white">Үндсэн зураг болгох</i></a>
                    </td>
                    <td>
                    <span class="size">{%=o.formatFileSize(file.size)%}</span>
                    </td>
                    <td>
                    {% if (file.deleteUrl) { %}
                    <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
                    <i class="glyphicon glyphicon-trash"></i>
                    <span>Устгах</span>
                    </button>
                    <input type="checkbox" name="delete" value="1" class="toggle">
                    {% } else { %}
                    <button class="btn btn-warning cancel">
                    <i class="glyphicon glyphicon-ban-circle"></i>
                    <span>Болих</span>
                    </button>
                    {% } %}
                    </td>
                    </tr>
                    {% } %}
                </script>
            </div>
        </div>

        <div id="blueimp-gallery" class="blueimp-gallery blueimp-gallery-controls" data-filter=":even">
            <div class="slides"></div>
            <h3 class="title"></h3>
            <a class="prev">‹</a>
            <a class="next">›</a>
            <a class="close">×</a>
            <a class="play-pause"></a>
            <ol class="indicator"></ol>
        </div>
    </div>
</div>

<div class="portlet box green">
    <div class="portlet-title">
        <div class="caption"><i class="icon-reorder"></i>Бараа бүртгэх</div>
        <div class="tools">
            <a href="javascript:;" class="collapse"></a>
        </div>
    </div>

    <div class="portlet-body">
        <form class="form-horizontal" method="POST" enctype="multipart/form-data" id="form" autocomplete="off">
            <input type="hidden" name="id" value="<?php echo $id ? $id : 0 ?>" />
            <?php echo $form['photos']->render(); ?>

            <div class="control-group <?php if ($form['name']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['name']->renderId() ?>"><?php echo $form['name']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['name']->render(); ?>
                    <?php if ($form['name']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['name']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['category_id']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['category_id']->renderId() ?>"><?php echo $form['category_id']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['category_id']->render(); ?>
                    <?php if ($form['category_id']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['category_id']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['type']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['type']->renderId() ?>"><?php echo $form['type']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['type']->render(); ?>
                    <?php if ($form['type']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['type']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['code']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['code']->renderId() ?>"><?php echo $form['code']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['code']->render(); ?>
                    <?php if ($form['code']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['code']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['price']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['price']->renderId() ?>"><?php echo $form['price']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['price']->render(); ?>
                    <?php if ($form['price']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['price']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['price_sale']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['price_sale']->renderId() ?>"><?php echo $form['price_sale']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['price_sale']->render(); ?>
                    <?php if ($form['price_sale']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['price_sale']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['sale_begin']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['sale_begin']->renderId() ?>"><?php echo $form['sale_begin']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['sale_begin']->render(); ?>
                    <?php if ($form['sale_begin']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['sale_begin']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['sale_end']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['sale_end']->renderId() ?>"><?php echo $form['sale_end']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['sale_end']->render(); ?>
                    <?php if ($form['sale_end']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['sale_end']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['manufacturer_id']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['manufacturer_id']->renderId() ?>"><?php echo $form['manufacturer_id']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['manufacturer_id']->render(); ?>
                    <?php if ($form['manufacturer_id']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['manufacturer_id']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['product_date']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['product_date']->renderId() ?>"><?php echo $form['product_date']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['product_date']->render(); ?>
                    <?php if ($form['product_date']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['product_date']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['intro_description']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['intro_description']->renderId() ?>"><?php echo $form['intro_description']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['intro_description']->render(); ?>
                    <?php if ($form['intro_description']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['intro_description']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group <?php if ($form['description']->hasError()): ?>error<?php endif; ?>">
                <label class="control-label" for="<?php echo $form['description']->renderId() ?>"><?php echo $form['description']->renderLabel() ?></label>
                <div class="controls">
                    <?php echo $form['description']->render(); ?>
                    <?php if ($form['description']->hasError()): ?>
                        <span class="help-inline"><?php echo $form['description']->renderError() ?></span>
                    <?php endif; ?>
                </div>
            </div>

            <div class="control-group">
                <div class="controls">
                    <button class="btn btn-success" type="submit">
                        <i class="icon-pencil icon-white"></i> Хадгалах
                    </button>
                </div>
            </div>

        </form>
    </div>
</div>
<script>
    $(function() {
        $('#fileupload').fileupload({
            url: '<?php echo $uploaderUrl ?>',
            maxFileSize: 10000000 //10MB
        }).on('fileuploadsubmit', function(e, data) {
            data.formData = data.context.find(':input').serializeArray();
        }).on('fileuploadcompleted', function(e, data) {
            var photos = $('.photo_ids').map(function() {
                return $(this).val();
            }).get().join();
            $('#product_photos').val(photos);
        });

//        $('#fileupload').addClass('fileupload-processing');
        $.ajax({
            url: $('#fileupload').fileupload('option', 'url'),
            dataType: 'json',
            context: $('#fileupload')[0],
            disableImageResize: /Android(?!.*Chrome)|Opera/
                    .test(window.navigator.userAgent),
            maxFileSize: 5000000,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i
        }).always(function() {
            $(this).removeClass('fileupload-processing');
        }).done(function(result) {
            $(this).fileupload('option', 'done')
                    .call(this, $.Event('done'), {result: result});
        });

        App.initUniform('.fileupload-toggle-checkbox');
    });

    function makeCover(ele, id) {
        $.ajax({
            url: '<?php echo $makeCoverUrl ?>/' + id,
            type: "GET"
        }).done(function(res) {
            if (res === '1') {
                $('.cover-maker').each(function() {
                    if (this === ele) {
                        $(this).hide();
                    } else {
                        $(this).show();
                    }
                });
            } else {
                alert('Үндсэн зураг болгов явцад алдаа гарлаа.');
            }
        });
    }
</script>
